package com.evernote.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.evernote.db.DBUtil;
import com.evernote.entity.Content;
import com.evernote.entity.NoteBook;
   
/**
 * 笔记本 dao类
 * @author zhang
 * 
 */
public class NoteBookDao extends BaseDao {
	
	/**
	 * 添加默认笔记本 新注册用户用
	 * @param n
	 * @return 返回1成功 0失败
	 */
	public int addDefaultNotebook(NoteBook n) {
		int count = 0;
		try {
			connection = DBUtil.getConnection();
			String sql = "insert into notebook(name, Uid) value(?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, n.getName());
			preparedStatement.setInt(2, n.getUid());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return count;
	}
	
	/**
	 * 获取用户所有笔记本和笔记本中笔记的数量
	 * @param n
	 * @return
	 */
	public List<NoteBook> findAllNotebookByUid(NoteBook n) {
		List<NoteBook> list = new ArrayList<>();
		try {
			connection = DBUtil.getConnection();
			String sql = "select * from notebook where Uid = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, n.getUid());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				NoteBook nb = new NoteBook();
				nb.setId(resultSet.getInt("id"));
				nb.setName(resultSet.getString("name"));
				nb.setUid(resultSet.getInt("Uid"));
				list.add(nb);
			}
			preparedStatement = connection.prepareStatement("select Nid,count(*) as count from content where Uid = ? group by Nid");
			preparedStatement.setInt(1, n.getUid());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				for (NoteBook noteBook : list) {
					if(noteBook.getId() == resultSet.getInt("Nid")) {
						noteBook.setNum(resultSet.getInt("count"));
					}
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return list;
	}
	
	/**
	 * 获取用户默认笔记本的id
	 * @param c
	 * @return int id
	 */
	public int findDefaultNoteIdByUid(NoteBook n) {
		int Nid = 0;
		try {
			connection = DBUtil.getConnection();
			String sql = "select id from notebook where Uid = ? and name = '默认'";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, n.getUid());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				Nid = resultSet.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return Nid;
	}
	
	/**
	 * 查询笔记本名是否存在
	 * @param n
	 * @return true存在 false不存在
	 */
	public boolean isExist(NoteBook n) {
		boolean flag = false;
		try {
			int count = 0;
			connection = DBUtil.getConnection();
			String sql = "select count(*) as count from notebook where Uid = ? and name = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, n.getUid());
			preparedStatement.setString(2, n.getName());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				count = resultSet.getInt("count");
			}
			if(count != 0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return flag;
	}
	
	/**
	 * 新增笔记本并返回其id
	 * @param n
	 * @return 成功将返回新建的笔记本的id 失败返回0
	 */
	public int newNotebook(NoteBook n) {
		int count = 0;
		int Nid = 0;
		
		try {
			connection = DBUtil.getConnection();
			String sql = "insert into notebook(name, Uid) value(?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, n.getName());
			preparedStatement.setInt(2, n.getUid());
			count = preparedStatement.executeUpdate();
			if(count != 0) {
				sql = "select last_insert_id() as Nid";
				preparedStatement = connection.prepareStatement(sql);
				resultSet = preparedStatement.executeQuery();
				while(resultSet.next()) {
					Nid = resultSet.getInt("Nid");
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return Nid;
	}
	
	/**
	 * 根据id查询笔记本名
	 * @param n
	 * @return
	 */
	public String findNameById(NoteBook n) {
		String name = "";
		try {
			connection = DBUtil.getConnection();
			String sql = "select name from notebook where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, n.getId());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				name = resultSet.getString("name");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return name;
	}
	
	/**
	 * 删除笔记本
	 * @param n
	 * @return 失败返回0
	 */
	public int DeleteNotebookById(NoteBook n) {
		int count = 0;
		try {
			connection = DBUtil.getConnection();
			String sql = "delete from notebook where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, n.getId());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return count;
	}
	
}
